package com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import java.util.List;

/**
 * 书相关横向滚动适配器
 * Created by wcy on 18/5/17.
 */
public class StyleFourAdapter extends BaseQuickAdapter<BookInfo> {

    private String mType;

    public StyleFourAdapter(Context context, List<BookInfo> data, String type) {
        super(context, data);
        mType = type;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_ad_selected_item_four_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, BookInfo item, int position) {
        if (item.adview != null) {
            ViewParent parent = item.adview.getParent();
            if (parent != null) {
                ViewGroup viewGroup = (ViewGroup) parent;
                viewGroup.removeView(item.adview);
            }
            ViewGroup container = holder.getView(R.id.root);
            container.removeAllViews();
            container.addView(item.adview);
            int height = container.getHeight();
            int width = container.getWidth();
            Log.i("test", "height " + height + " width " + width);
            return;
        }
        holder.setVisible(R.id.label, true);
        holder.setText(R.id.book_title, item.book_title).setText(R.id.label, BookUtils.getBookLabel(item));
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        holder.getConvertView().setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.book_id);
            /**
             * 埋点
             */
            BookCityUtils.takeIf(mType, Constants.BookCityModuleType.TYPE_SELECTED_ZHONGJIYOUHUO, () -> {
                AdPresenter.pvuvStatics(getData(), b -> b.adview != null, position,
                        R.string.SCTUIJIAN4_POSITION1,
                        R.string.SCTUIJIAN4_POSITION2,
                        R.string.SCTUIJIAN4_POSITION3,
                        R.string.SCTUIJIAN4_POSITION4,
                        R.string.SCTUIJIAN4_POSITION5,
                        R.string.SCTUIJIAN4_POSITION6);
            });
            BookCityUtils.takeIf(mType, Constants.BookCityModuleType.TYPE_SELECTED_XINSHUQIANGXIAN, () -> {
                AdPresenter.pvuvStatics(getData(), b -> b.adview != null,position,
                        R.string.SCTUIJIAN9_POSITION1,
                        R.string.SCTUIJIAN9_POSITION2,
                        R.string.SCTUIJIAN9_POSITION3,
                        R.string.SCTUIJIAN9_POSITION4,
                        R.string.SCTUIJIAN9_POSITION5,
                        R.string.SCTUIJIAN9_POSITION6);
            });
        });
    }

    @Override
    public int getItemViewType(int position) {
        int type = super.getItemViewType(position);
        if (type == 0) {
            BookInfo info = getData().get(position - getHeaderViewsCount());
            if (info.adview != null) {
                type = EMPTY_CONTAINER;
            }
        }
        return type;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mParentView == null) {
            mParentView = parent;
        }
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case LOADING_VIEW:
                baseViewHolder = new BaseViewHolder(mLoadingView);
                break;
            case HEADER_VIEW:
                baseViewHolder = new BaseViewHolder(mHeaderView);
                break;
            case EMPTY_VIEW:
                baseViewHolder = new BaseViewHolder(mEmptyView);
                break;
            case FOOTER_VIEW:
                baseViewHolder = new BaseViewHolder(mFooterView);
                break;
            case EMPTY_CONTAINER:
                View view = mLayoutInflater.inflate(R.layout.ad_empty_container, parent, false);
                baseViewHolder = new BaseViewHolder(view);
                break;
            default:
                baseViewHolder = onCreateDefViewHolder(parent, viewType);
                // 设置用于单项刷新的tag标识
                baseViewHolder.itemView.setTag(R.id.view_holder_tag, baseViewHolder);
                _initItemClickListener(baseViewHolder);
                break;
        }
        return baseViewHolder;
    }

}
